﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DiscoveryLogic.UI.Charts.Converters
{
    public class NullableThicknessConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return true;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            String[] thickness = value.ToString().Split(',');

            Thickness markerBorderThickness;

            if (thickness.Length == 1)
            {
                markerBorderThickness = new Thickness(Convert.ToDouble(thickness[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[0], System.Globalization.CultureInfo.InvariantCulture));
            }
            else if (thickness.Length == 4)
            {
                markerBorderThickness = new Thickness(Convert.ToDouble(thickness[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[1], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[2], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[3], System.Globalization.CultureInfo.InvariantCulture));
            }
            else
                throw new NotSupportedException("Incorrect property value of property type Thickness");

            return markerBorderThickness;
        }
    }
}
